package com.noble.glamour.module.logic.application;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.multidex.MultiDexApplication;

import com.alibaba.sdk.android.httpdns.HttpDnsService;
import com.noble.library.utils.ScreenUtils;
import com.noble.library.utils.HttpDnsInstance;

/**
 * Application
 * Created by ZRP on 2016/9/8.
 */
public class App extends MultiDexApplication {

    public static Context context;
    public static Activity activity;

    /**
     * 登录用户的uid（供运行时程序内调用）
     */
    public static long uid = 0;//全局uid，避免重复从本地获取

    /**
     * 发送需要登录信息的Http请求使用。
     */
    public static String cookie = "";

    /**
     * 用户是否已经登录。该值暂时无效
     */
    public static boolean isLogin = false;

    /**
     * 是否锁屏中
     */
    public static volatile AppKeyguard isKeyguard = AppKeyguard.KG_INIT;

    private static PActivityLifecycleCallbacks lifecycleCallbacks;
    public static long t;
    public static HttpDnsService httpdns;

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onCreate() {
        super.onCreate();

//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }

        t = System.currentTimeMillis();
        context = getApplicationContext();

        lifecycleCallbacks = new PActivityLifecycleCallbacks();
        registerActivityLifecycleCallbacks(lifecycleCallbacks);

        ModuleMgr.initModule(context);
        httpdns = HttpDnsInstance.getInstance(context).getHttpDNS();//只能保证一个实例
    }

    /**
     * @return 获取进程生命周期回调
     */
    public static PActivityLifecycleCallbacks getLifecycleCallbacks() {
        return lifecycleCallbacks;
    }

    /**
     * @return 获取当前展示的activity对象，如果activity为null则返回applicationContext
     */
    public static Context getActivity() {
        return activity == null ? context : activity;
    }

    public static Context getContext() {
        return context;
    }

    /**
     * @return 判断最后的Activity是否属于前台显示
     */
    public static boolean isForeground() {
        return lifecycleCallbacks.isForeground();
    }

    /**
     * 应用锁屏状态
     */
    public enum AppKeyguard {
        KG_INIT,        //初始化
        KG_SCREEN_OFF,  //锁屏中
        KG_SCREEN_ON    //开屏中
    }

    // -----------------------------------------------------

//    private static CacheComponent cacheComponent;
//
//    public static CacheComponent getCacheComponent() {
//        return cacheComponent;
//    }
//
//    /**
//     * 缓存初始化
//     */
//    private void initAppComponent() {
//        cacheComponent = DaggerCacheComponent.builder()
//                .cacheModule(new CacheModule((Application) getContext()))
//                .dBCache(new DBCache())
//                .build();
//    }
}
